package cn.wenhe9.yygh.user.api;

import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.common.utils.AuthContextHolder;
import cn.wenhe9.yygh.model.user.Patient;
import cn.wenhe9.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author DuJinliang
 * 2022/4/6
 */
@Api(tags = "就诊人管理接口")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {
    @Resource
    private PatientService patientService;

    /**
     * 获取就诊人列表
     * @param request HttpServletRequest
     * @return ResultResponse
     */
    @ApiOperation("获取就诊人列表")
    @GetMapping("/auth/list")
    public ResultResponse findAll(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        return patientService.findAllByUserId(userId);
    }

    /**
     * 添加就诊人
     * @param patient Patient
     * @param request HttpServletRequest
     * @return ResultResponse
     */
    @ApiOperation("添加就诊人")
    @PostMapping("/auth")
    public ResultResponse savePatient(@RequestBody Patient patient, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        boolean flag = patientService.save(patient);
        if (flag){
            return ResultResponse.ok();
        }
        return ResultResponse.fail();
    }

    /**
     * 根据id获取就诊人信息
     * @param id Long
     * @return ResultResponse
     */
    @ApiOperation("根据id获取就诊人信息")
    @GetMapping("/auth/{id}")
    public ResultResponse getPatientById(@PathVariable(name = "id") Long id){
        Patient patient = patientService.getPatientById(id);
        return ResultResponse.ok(patient);
    }

    /**
     * 修改就诊人
     * @param patient Patient
     * @return ResultResponse
     */
    @ApiOperation("修改就诊人")
    @PutMapping("/auth")
    public ResultResponse updatePatient(@RequestBody Patient patient){
        boolean flag = patientService.updateById(patient);
        if (flag){
            return ResultResponse.ok();
        }
        return ResultResponse.fail();
    }

    /**
     * 删除就诊人
     * @param id Long
     * @return ResultResponse
     */
    @ApiOperation("删除就诊人")
    @DeleteMapping("/auth/{id}")
    public ResultResponse removePatient(@PathVariable(name = "id") Long id){
        boolean flag = patientService.removeById(id);
        if (flag){
            return ResultResponse.ok();
        }
        return ResultResponse.fail();
    }

    /**
     * 根据就诊人id获取就诊人信息
     * @param id Long
     * @return Patient
     */
    @GetMapping("/inner/get/{id}")
    public Patient getPatientOrder(@PathVariable(name = "id") Long id){
        return patientService.getPatientById(id);
    }
}
